package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.base.bean.AddressBean;
import com.whmnx.base.bean.PagerInfo;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.activity.MyLocationActivity;
import com.whmnx.my.ui.fragment.MyAddLoctionFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/15 0015 18:29
 * version: 1.0
 */
public class LoctionModel extends ToolbarViewModel {
    public LoctionModel(@NonNull Application application) {
        super(application);
    }
    private MyLocationActivity mActivity;

    private  Disposable subscribe;


    public void initToolbar(MyLocationActivity myLocationActivity) {
        this.mActivity = myLocationActivity;
        setTitleText("选择地址");
        setRightText("新增");
        setRightTextVisible(View.VISIBLE);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getLoction();
        initView();
    }

    private void initView() {
        subscribe = RxBus.getDefault().toObservable(String.class)
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String object){
                        if (object.equals(EventConstants.ADDRESS)){
                            getLoction();
                        }
                    }
                });
        RxSubscriptions.add(subscribe);
    }

    private void getLoction() {
        PagerInfo pagerInfo =   new PagerInfo();
        pagerInfo.setPageIndex(1);
        pagerInfo.setPageSize(10);
        Map<String, Object> params = new HashMap<>();
        params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("pageIndex", 1);
        params.put("pageSize", 10);

        HttpClient.post(mActivity, Constants.ADDRESSLIST, params,new CallBack<List<AddressBean>>() {
            @Override
            public void onSuccess(List<AddressBean> addressBeans) {
                observableList.clear();
                for (AddressBean addressBean : addressBeans) {
                    observableList.add(new LoctionItemViewModel(LoctionModel.this,addressBean));
                }
            }
        });
    }

    //给RecyclerView添加ObservableList
    public ObservableList observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<LoctionItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_loction);

    @Override
    protected void rightTextOnClick() {
        startContainerActivity(MyAddLoctionFragment.class.getCanonicalName());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxSubscriptions.remove(subscribe);
    }
}
